/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.common.dfs;

import com.alibaba.fastjson.util.Base64;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Component
public class FastDFSClientWrapper {

    @Autowired
    private FastFileStorageClient storageClient;


    @Autowired
    private FileServer fileServer;

    /**
     * 上传文件
     *
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
    	return fileServer.uploadFile(file);
    }

    public String uploadFile(InputStream inputStream, long fileSize, String fileExtName) throws IOException {
    	return fileServer.uploadFile(inputStream, fileSize, fileExtName);
    }

    /**
     * 将一段字符串生成一个文件上传
     *
     * @param content       文件内容
     * @param fileExtension
     * @return
     */
    public String uploadFile(String content, String fileExtension) {
        byte[] buff = content.getBytes(StandardCharsets.UTF_8);
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
        return getResAccessUrl(storePath);
    }

    /**
     * 将base64编码字符串转换为图片
     *
     * @param pic
     * @return
     */
    public String uploadFile(String pic) {
        byte[] buff = Base64.decodeFast(pic);
        for (int i = 0; i < buff.length; ++i) {
            if (buff[i] < 0) {
                // 调整异常数据
                buff[i] += (byte) 256;
            }
        }
        ByteArrayInputStream stream = new ByteArrayInputStream(buff);
        StorePath storePath = storageClient.uploadFile(stream, buff.length, "png", null);
        return getResAccessUrl(storePath);
    }

    // 封装图片完整URL地址
    private String getResAccessUrl(StorePath storePath) {
//        String fileUrl = "http://" + fdfsConfig.getResHost() + ":" + fdfsConfig.getStoragePort() + "/"
//                + storePath.getFullPath();
        String fileUrl = "/"+ storePath.getFullPath();
        return fileUrl;
    }

    /**
     * 删除文件
     *
     * @param fileUrl 文件访问地址
     * @return
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        fileServer.deleteFile(fileUrl);
        /*try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            logger.warn(e.getMessage());
        } catch (FdfsServerException fdfsServerException) {
            logger.warn(fdfsServerException.getMessage());
        }*/
    }

    /**
     * 下载文件
     * @param fileUrl
     * @return
     */
    public InputStream downloadFile(String fileUrl) {
        return fileServer.downloadFile(fileUrl);
    }
}
